home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / cc / sprite / gcc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-06-28  |  47.2 KB  |  1,938 lines

  1. /* Compiler driver program that can handle many languages.
  2.    Copyright (C) 1987,1989 Free Software Foundation, Inc.
  3.  
  4. This file is part of GNU CC.
  5.  
  6. GNU CC is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 1, or (at your option)
  9. any later version.
  10.  
  11. GNU CC is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with GNU CC; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  
  20. This paragraph is here to try to keep Sun CC from dying.
  21. The number of chars here seems crucial!!!!  */
  22.  
  23. void record_temp_file ();
  24.  
  25. /* This program is the user interface to the C compiler and possibly to
  26. other compilers.  It is used because compilation is a complicated procedure
  27. which involves running several programs and passing temporary files between
  28. them, forwarding the users switches to those programs selectively,
  29. and deleting the temporary files at the end.
  30.  
  31. CC recognizes how to compile each input file by suffixes in the file names.
  32. Once it knows which kind of compilation to perform, the procedure for
  33. compilation is specified by a string called a "spec".
  34.  
  35. Specs are strings containing lines, each of which (if not blank)
  36. is made up of a program name, and arguments separated by spaces.
  37. The program name must be exact and start from root, since no path
  38. is searched and it is unreliable to depend on the current working directory.
  39. Redirection of input or output is not supported; the subprograms must
  40. accept filenames saying what files to read and write.
  41.  
  42. In addition, the specs can contain %-sequences to substitute variable text
  43. or for conditional text.  Here is a table of all defined %-sequences.
  44. Note that spaces are not generated automatically around the results of
  45. expanding these sequences; therefore, you can concatenate them together
  46. or with constant text in a single argument.
  47.  
  48.  %%    substitute one % into the program name or argument.
  49.  %i     substitute the name of the input file being processed.
  50.  %b     substitute the basename of the input file being processed.
  51.     This is the substring up to (and not including) the last period.
  52.  %g     substitute the temporary-file-name-base.  This is a string chosen
  53.     once per compilation.  Different temporary file names are made by
  54.     concatenation of constant strings on the end, as in `%g.s'.
  55.     %g also has the same effect of %d.
  56.  %d    marks the argument containing or following the %d as a
  57.     temporary file name, so that that file will be deleted if CC exits
  58.     successfully.  Unlike %g, this contributes no text to the argument.
  59.  %w    marks the argument containing or following the %w as the
  60.     "output file" of this compilation.  This puts the argument
  61.     into the sequence of arguments that %o will substitute later.
  62.  %o    substitutes the names of all the output files, with spaces
  63.     automatically placed around them.  You should write spaces
  64.     around the %o as well or the results are undefined.
  65.     %o is for use in the specs for running the linker.
  66.     Input files whose names have no recognized suffix are not compiled
  67.     at all, but they are included among the output files, so they will
  68.     be linked.
  69.  %p    substitutes the standard macro predefinitions for the
  70.     current target machine.  Use this when running cpp.
  71.  %P    like %p, but puts `__' before and after the name of each macro.
  72.     This is for ANSI C.
  73.  %s     current argument is the name of a library or startup file of some sort.
  74.         Search for that file in a standard list of directories
  75.     and substitute the full pathname found.
  76.  %eSTR  Print STR as an error message.  STR is terminated by a newline.
  77.         Use this when inconsistent options are detected.
  78.  %a     process ASM_SPEC as a spec.
  79.         This allows config.h to specify part of the spec for running as.
  80.  %l     process LINK_SPEC as a spec.
  81.  %L     process LIB_SPEC as a spec.
  82.  %S     process STARTFILE_SPEC as a spec.  A capital S is actually used here.
  83.  %c    process SIGNED_CHAR_SPEC as a spec.
  84.  %C     process CPP_SPEC as a spec.  A capital C is actually used here.
  85.  %1    process CC1_SPEC as a spec.
  86.  %{S}   substitutes the -S switch, if that switch was given to CC.
  87.     If that switch was not specified, this substitutes nothing.
  88.     Here S is a metasyntactic variable.
  89.  %{S*}  substitutes all the switches specified to CC whose names start
  90.     with -S.  This is used for -o, -D, -I, etc; switches that take
  91.     arguments.  CC considers `-o foo' as being one switch whose
  92.     name starts with `o'.  %{o*} would substitute this text,
  93.     including the space; thus, two arguments would be generated.
  94.  %{S:X} substitutes X, but only if the -S switch was given to CC.
  95.  %{!S:X} substitutes X, but only if the -S switch was NOT given to CC.
  96.  %{|S:X} like %{S:X}, but if no S switch, substitute `-'.
  97.  %{|!S:X} like %{!S:X}, but if there is an S switch, substitute `-'.
  98.  
  99. The conditional text X in a %{S:X} or %{!S:X} construct may contain
  100. other nested % constructs or spaces, or even newlines.
  101. They are processed as usual, as described above.
  102.  
  103. The character | is used to indicate that a command should be piped to
  104. the following command, but only if -pipe is specified.
  105.  
  106. Note that it is built into CC which switches take arguments and which
  107. do not.  You might think it would be useful to generalize this to
  108. allow each compiler's spec to say which switches take arguments.  But
  109. this cannot be done in a consistent fashion.  CC cannot even decide
  110. which input files have been specified without knowing which switches
  111. take arguments, and it must know which input files to compile in order
  112. to tell which compilers to run.
  113.  
  114. CC also knows implicitly that arguments starting in `-l' are to
  115. be treated as compiler output files, and passed to the linker in their proper
  116. position among the other output files.
  117.  
  118. */
  119.  
  120. #include <stdio.h>
  121. #include <string.h>
  122. #include <sys/types.h>
  123. #include <signal.h>
  124. #include <sys/file.h>
  125. #include <alloca.h>
  126.  
  127. /* #include "config.h" */
  128. #include "obstack.h"
  129. #include "gvarargs.h"
  130.  
  131. #ifdef USG
  132. #define R_OK 4
  133. #define W_OK 2
  134. #define X_OK 1
  135. #define vfork fork
  136. #endif /* USG */
  137.  
  138. #define obstack_chunk_alloc xmalloc
  139. #define obstack_chunk_free free
  140. extern int xmalloc ();
  141. extern void free ();
  142.  
  143. /* If a stage of compilation returns an exit status >= 1,
  144.    compilation of that file ceases.  */
  145.  
  146. #define MIN_FATAL_STATUS 1
  147.  
  148. /* This is the obstack which we use to allocate many strings.  */
  149.  
  150. struct obstack obstack;
  151.  
  152. char *handle_braces ();
  153. char *save_string ();
  154. char *concat ();
  155. int do_spec ();
  156. int do_spec_1 ();
  157. char *find_file ();
  158. static char *find_exec_file ();
  159. void validate_switches ();
  160. void validate_all_switches ();
  161. void fancy_abort ();
  162. static int cplusplus;
  163.  
  164. /*
  165.  * Generate tables containing specs for all known target machines:
  166.  */
  167.  
  168. #include "specs.h"
  169. /* This defines which switch letters take arguments.  */
  170.  
  171. #ifndef SWITCH_TAKES_ARG
  172. #define SWITCH_TAKES_ARG(CHAR)      \
  173.   ((CHAR) == 'D' || (CHAR) == 'U' || (CHAR) == 'o' \
  174.    || (CHAR) == 'e' || (CHAR) == 'T' || (CHAR) == 'u' \
  175.    || (CHAR) == 'I' || (CHAR) == 'Y' || (CHAR) == 'm' \
  176.    || (CHAR) == 'L' || (CHAR) == 'i' || (CHAR) == 'A' \
  177.    || (CHAR) == 'B')
  178. #endif
  179.  
  180. /* This defines which multi-letter switches take arguments.  */
  181.  
  182. #ifndef WORD_SWITCH_TAKES_ARG
  183. #define WORD_SWITCH_TAKES_ARG(STR) (!strcmp (STR, "Tdata"))
  184. #endif
  185.  
  186. /* Accumulate a command (program name and args), and run it.  */
  187.  
  188. /* Vector of pointers to arguments in the current line of specifications.  */
  189.  
  190. char **argbuf;
  191.  
  192. /* Number of elements allocated in argbuf.  */
  193.  
  194. int argbuf_length;
  195.  
  196. /* Number of elements in argbuf currently in use (containing args).  */
  197.  
  198. int argbuf_index;
  199.  
  200. /* Number of commands executed so far.  */
  201.  
  202. int execution_count;
  203.  
  204. /* Flag indicating whether we should print the command and arguments */
  205.  
  206. unsigned char vflag;
  207.  
  208. /* Name with which this program was invoked.  */
  209.  
  210. char *programname;
  211.  
  212. /* User-specified -B prefix to attach to command names,
  213.    or 0 if none specified.  */
  214.  
  215. char *user_exec_prefix = 0;
  216.  
  217. /* Environment-specified prefix to attach to command names,
  218.    or 0 if none specified.  */
  219.  
  220. /* Default prefixes to attach to command names.  If a name contains a
  221.     "%s", then it will be replaced with the type of machine on which
  222.     we're running, to get the correct binaries for this machine. */
  223.  
  224. char *standard_exec_prefix = "/sprite/lib/gcc/%s.md/";
  225. char *standard_exec_prefix_1 = "/sprite/cmds.%s/";
  226.  
  227. char *standard_startfile_prefix = "/sprite/lib/%s.md/";
  228. char *standard_startfile_prefix_1 = "/sprite/lib/gcc/%s.md/";
  229.  
  230. /* Type of machine on which we're running (used to select correct
  231.     binaries for programs like cpp). */
  232.  
  233. char *machine;
  234. int machine_length;        /* No. of characters in machine. */
  235.  
  236. char *env_exec_prefix = 0;
  237.  
  238. /* Suffix to attach to directories searched for commands.  */
  239.  
  240. char *machine_suffix = 0;
  241.  
  242. /* Default prefixes to attach to command names.  */
  243.  
  244. #ifndef STANDARD_EXEC_PREFIX
  245. #define STANDARD_EXEC_PREFIX "/usr/local/lib/gcc-"
  246. #endif /* !defined STANDARD_EXEC_PREFIX */
  247.  
  248. #ifndef STANDARD_STARTFILE_PREFIX
  249. #define STANDARD_STARTFILE_PREFIX "/usr/local/lib/"
  250. #endif /* !defined STANDARD_STARTFILE_PREFIX */
  251.  
  252. /* Clear out the vector of arguments (after a command is executed).  */
  253.  
  254. void
  255. clear_args ()
  256. {
  257.   argbuf_index = 0;
  258. }
  259.  
  260. /* Add one argument to the vector at the end.
  261.    This is done when a space is seen or at the end of the line.
  262.    If DELETE_ALWAYS is nonzero, the arg is a filename
  263.     and the file should be deleted eventually.
  264.    If DELETE_FAILURE is nonzero, the arg is a filename
  265.     and the file should be deleted if this compilation fails.  */
  266.  
  267. void
  268. store_arg (arg, delete_always, delete_failure)
  269.      char *arg;
  270.      int delete_always, delete_failure;
  271. {
  272.   if (argbuf_index + 1 == argbuf_length)
  273.     {
  274.       argbuf = (char **) realloc (argbuf, (argbuf_length *= 2) * sizeof (char *));
  275.     }
  276.  
  277.   argbuf[argbuf_index++] = arg;
  278.   argbuf[argbuf_index] = 0;
  279.  
  280.   if (delete_always || delete_failure)
  281.     record_temp_file (arg, delete_always, delete_failure);
  282. }
  283.  
  284. /* Record the names of temporary files we tell compilers to write,
  285.    and delete them at the end of the run.  */
  286.  
  287. /* This is the common prefix we use to make temp file names.
  288.    It is chosen once for each run of this program.
  289.    It is substituted into a spec by %g.
  290.    Thus, all temp file names contain this prefix.
  291.    In practice, all temp file names start with this prefix.
  292.  
  293.    This prefix comes from the envvar TMPDIR if it is defined;
  294.    otherwise, from the P_tmpdir macro if that is defined;
  295.    otherwise, in /usr/tmp or /tmp.  */
  296.  
  297. char *temp_filename;
  298.  
  299. /* Length of the prefix.  */
  300.  
  301. int temp_filename_length;
  302.  
  303. /* Define the list of temporary files to delete.  */
  304.  
  305. struct temp_file
  306. {
  307.   char *name;
  308.   struct temp_file *next;
  309. };
  310.  
  311. /* Queue of files to delete on success or failure of compilation.  */
  312. struct temp_file *always_delete_queue;
  313. /* Queue of files to delete on failure of compilation.  */
  314. struct temp_file *failure_delete_queue;
  315.  
  316. /* Record FILENAME as a file to be deleted automatically.
  317.    ALWAYS_DELETE nonzero means delete it if all compilation succeeds;
  318.    otherwise delete it in any case.
  319.    FAIL_DELETE nonzero means delete it if a compilation step fails;
  320.    otherwise delete it in any case.  */
  321.  
  322. void
  323. record_temp_file (filename, always_delete, fail_delete)
  324.      char *filename;
  325.      int always_delete;
  326.      int fail_delete;
  327. {
  328.   register char *name;
  329.   name = (char *) xmalloc (strlen (filename) + 1);
  330.   strcpy (name, filename);
  331.  
  332.   if (always_delete)
  333.     {
  334.       register struct temp_file *temp;
  335.       for (temp = always_delete_queue; temp; temp = temp->next)
  336.     if (! strcmp (name, temp->name))
  337.       goto already1;
  338.       temp = (struct temp_file *) xmalloc (sizeof (struct temp_file));
  339.       temp->next = always_delete_queue;
  340.       temp->name = name;
  341.       always_delete_queue = temp;
  342.     already1:;
  343.     }
  344.  
  345.   if (fail_delete)
  346.     {
  347.       register struct temp_file *temp;
  348.       for (temp = failure_delete_queue; temp; temp = temp->next)
  349.     if (! strcmp (name, temp->name))
  350.       goto already2;
  351.       temp = (struct temp_file *) xmalloc (sizeof (struct temp_file));
  352.       temp->next = failure_delete_queue;
  353.       temp->name = name;
  354.       failure_delete_queue = temp;
  355.     already2:;
  356.     }
  357. }
  358.  
  359. /* Delete all the temporary files whose names we previously recorded.  */
  360.  
  361. void
  362. delete_temp_files ()
  363. {
  364.   register struct temp_file *temp;
  365.  
  366.   for (temp = always_delete_queue; temp; temp = temp->next)
  367.     {
  368. #ifdef DEBUG
  369.       int i;
  370.       printf ("Delete %s? (y or n) ", temp->name);
  371.       fflush (stdout);
  372.       i = getchar ();
  373.       if (i != '\n')
  374.     while (getchar () != '\n') ;
  375.       if (i == 'y' || i == 'Y')
  376. #endif /* DEBUG */
  377.     {
  378.       if (unlink (temp->name) < 0)
  379.         if (vflag)
  380.           perror_with_name (temp->name);
  381.     }
  382.     }
  383.  
  384.   always_delete_queue = 0;
  385. }
  386.  
  387. /* Delete all the files to be deleted on error.  */
  388.  
  389. void
  390. delete_failure_queue ()
  391. {
  392.   register struct temp_file *temp;
  393.  
  394.   for (temp = failure_delete_queue; temp; temp = temp->next)
  395.     {
  396. #ifdef DEBUG
  397.       int i;
  398.       printf ("Delete %s? (y or n) ", temp->name);
  399.       fflush (stdout);
  400.       i = getchar ();
  401.       if (i != '\n')
  402.     while (getchar () != '\n') ;
  403.       if (i == 'y' || i == 'Y')
  404. #endif /* DEBUG */
  405.     {
  406.       if (unlink (temp->name) < 0)
  407.         if (vflag)
  408.           perror_with_name (temp->name);
  409.     }
  410.     }
  411. }
  412.  
  413. void
  414. clear_failure_queue ()
  415. {
  416.   failure_delete_queue = 0;
  417. }
  418.  
  419. /* Compute a string to use as the base of all temporary file names.
  420.    It is substituted for %g.  */
  421.  
  422. void
  423. choose_temp_base ()
  424. {
  425.   extern char *getenv ();
  426.   char *base = getenv ("TMPDIR");
  427.   int len;
  428.  
  429.   if (base == (char *)0)
  430.     {
  431. #ifdef P_tmpdir
  432.       if (access (P_tmpdir, R_OK | W_OK) == 0)
  433.     base = P_tmpdir;
  434. #endif
  435.       if (base == (char *)0)
  436.     {
  437.       if (access ("/usr/tmp", R_OK | W_OK) == 0)
  438.         base = "/usr/tmp/";
  439.       else
  440.         base = "/tmp/";
  441.     }
  442.     }
  443.  
  444.   len = strlen (base);
  445.   temp_filename = (char *) xmalloc (len + sizeof("/ccXXXXXX"));
  446.   strcpy (temp_filename, base);
  447.   if (len > 0 && temp_filename[len-1] != '/')
  448.     temp_filename[len++] = '/';
  449.   strcpy (temp_filename + len, "ccXXXXXX");
  450.  
  451.   mktemp (temp_filename);
  452.   temp_filename_length = strlen (temp_filename);
  453. }
  454.  
  455. /* Search for an execute file through our search path.
  456.    Return 0 if not found, otherwise return its name, allocated with malloc.  */
  457.  
  458. static char *
  459. find_exec_file (prog)
  460.      char *prog;
  461. {
  462.   int win = 0;
  463.   char *temp;
  464.   int size;
  465.  
  466.   size = strlen (standard_exec_prefix);
  467.   if (user_exec_prefix != 0 && strlen (user_exec_prefix) > size)
  468.     size = strlen (user_exec_prefix);
  469.   if (env_exec_prefix != 0 && strlen (env_exec_prefix) > size)
  470.     size = strlen (env_exec_prefix);
  471.   if (strlen (standard_exec_prefix_1) > size)
  472.     size = strlen (standard_exec_prefix_1);
  473.   size += strlen (prog) + machine_length + 1;
  474.   if (machine_suffix)
  475.     size += strlen (machine_suffix) + 1;
  476.   temp = (char *) xmalloc (size);
  477.  
  478.   /* Determine the filename to execute.  */
  479.  
  480.   if (user_exec_prefix)
  481.     {
  482.       sprintf (temp, user_exec_prefix, machine);        
  483.       strcat (temp, prog);
  484.       win = (access (temp, X_OK) == 0);
  485.     }
  486.  
  487.   if (!win && env_exec_prefix)
  488.     {
  489.       sprintf(temp, env_exec_prefix, machine);
  490.       strcat (temp, prog);
  491.       win = (access (temp, X_OK) == 0);
  492.     }
  493.  
  494.   if (!win)
  495.     {
  496.       sprintf (temp, standard_exec_prefix, machine);
  497.       strcat (temp, prog);
  498.       win = (access (temp, X_OK) == 0);
  499.     }
  500.  
  501.   if (!win)
  502.     {
  503.       sprintf (temp, standard_exec_prefix_1, machine);
  504.       strcat (temp, prog);
  505.       win = (access (temp, X_OK) == 0);
  506.     }
  507.  
  508.   if (win)
  509.     return temp;
  510.   else
  511.     return 0;
  512. }
  513.  
  514. /* stdin file number.  */
  515. #define STDIN_FILE_NO 0
  516.  
  517. /* stdout file number.  */
  518. #define STDOUT_FILE_NO 1
  519.  
  520. /* value of `pipe': port index for reading.  */
  521. #define READ_PORT 0
  522.  
  523. /* value of `pipe': port index for writing.  */
  524. #define WRITE_PORT 1
  525.  
  526. /* Pipe waiting from last process, to be used as input for the next one.
  527.    Value is STDIN_FILE_NO if no pipe is waiting
  528.    (i.e. the next command is the first of a group).  */
  529.  
  530. int last_pipe_input;
  531.  
  532. /* Fork one piped subcommand.  FUNC is the system call to use
  533.    (either execv or execvp).  ARGV is the arg vector to use.
  534.    NOT_LAST is nonzero if this is not the last subcommand
  535.    (i.e. its output should be piped to the next one.)  */
  536.  
  537. static int
  538. pexecute (func, program, argv, not_last)
  539.      char *program;
  540.      int (*func)();
  541.      char *argv[];
  542.      int not_last;
  543. {
  544.   int pid;
  545.   int pdes[2];
  546.   int input_desc = last_pipe_input;
  547.   int output_desc = STDOUT_FILE_NO;
  548.  
  549.   /* If this isn't the last process, make a pipe for its output,
  550.      and record it as waiting to be the input to the next process.  */
  551.  
  552.   if (not_last)
  553.     {
  554.       if (pipe (pdes) < 0)
  555.     pfatal_with_name ("pipe");
  556.       output_desc = pdes[WRITE_PORT];
  557.       last_pipe_input = pdes[READ_PORT];
  558.     }
  559.   else
  560.     last_pipe_input = STDIN_FILE_NO;
  561.  
  562.   pid = vfork ();
  563.  
  564.   switch (pid)
  565.     {
  566.     case -1:
  567.       pfatal_with_name ("vfork");
  568.       break;
  569.  
  570.     case 0: /* child */
  571.       /* Move the input and output pipes into place, if nec.  */
  572.       if (input_desc != STDIN_FILE_NO)
  573.     {
  574.       close (STDIN_FILE_NO);
  575.       dup (input_desc);
  576.       close (input_desc);
  577.     }
  578.       if (output_desc != STDOUT_FILE_NO)
  579.     {
  580.       close (STDOUT_FILE_NO);
  581.       dup (output_desc);
  582.       close (output_desc);
  583.     }
  584.  
  585.       /* Close the parent's descs that aren't wanted here.  */
  586.       if (last_pipe_input != STDIN_FILE_NO)
  587.     close (last_pipe_input);
  588.  
  589.       /* Exec the program.  */
  590.       (*func) (program, argv);
  591.       perror_exec (program);
  592.       exit (-1);
  593.       /* NOTREACHED */
  594.  
  595.     default:
  596.       /* In the parent, after forking.
  597.      Close the descriptors that we made for this child.  */
  598.       if (input_desc != STDIN_FILE_NO)
  599.     close (input_desc);
  600.       if (output_desc != STDOUT_FILE_NO)
  601.     close (output_desc);
  602.  
  603.       /* Return child's process number.  */
  604.       return pid;
  605.     }
  606. }
  607.  
  608. /* Execute the command specified by the arguments on the current line of spec.
  609.    When using pipes, this includes several piped-together commands
  610.    with `|' between them.
  611.  
  612.    Return 0 if successful, -1 if failed.  */
  613.  
  614. int
  615. execute ()
  616. {
  617.   int i, j;
  618.   int n_commands;        /* # of command.  */
  619.   char *string;
  620.   struct command
  621.     {
  622.       char *prog;        /* program name.  */
  623.       char **argv;        /* vector of args.  */
  624.       int pid;            /* pid of process for this command.  */
  625.     };
  626.  
  627.   struct command *commands;    /* each command buffer with above info.  */
  628.  
  629.   /* Count # of piped commands.  */
  630.   for (n_commands = 1, i = 0; i < argbuf_index; i++)
  631.     if (strcmp (argbuf[i], "|") == 0)
  632.       n_commands++;
  633.  
  634.   /* Get storage for each command.  */
  635.   commands
  636.     = (struct command *) alloca (n_commands * sizeof (struct command));
  637.  
  638.   /* Split argbuf into its separate piped processes,
  639.      and record info about each one.
  640.      Also search for the programs that are to be run.  */
  641.  
  642.   commands[0].prog = argbuf[0]; /* first command.  */
  643.   commands[0].argv = &argbuf[0];
  644.   string = find_exec_file (commands[0].prog);
  645.   if (string)
  646.     commands[0].argv[0] = string;
  647.  
  648.   for (n_commands = 1, i = 0; i < argbuf_index; i++)
  649.     if (strcmp (argbuf[i], "|") == 0)
  650.       {                /* each command.  */
  651.     argbuf[i] = 0;    /* termination of command args.  */
  652.     commands[n_commands].prog = argbuf[i + 1];
  653.     commands[n_commands].argv = &argbuf[i + 1];
  654.     string = find_exec_file (commands[n_commands].prog);
  655.     if (string)
  656.       commands[n_commands].argv[0] = string;
  657.     n_commands++;
  658.       }
  659.  
  660.   argbuf[argbuf_index] = 0;
  661.  
  662.   /* If -v, print what we are about to do, and maybe query.  */
  663.  
  664.   if (vflag)
  665.     {
  666.       /* Print each piped command as a separate line.  */
  667.       for (i = 0; i < n_commands ; i++)
  668.     {
  669.       char **j;
  670.  
  671.       for (j = commands[i].argv; *j; j++)
  672.         fprintf (stderr, " %s", *j);
  673.  
  674.       /* Print a pipe symbol after all but the last command.  */
  675.       if (i + 1 != n_commands)
  676.         fprintf (stderr, " |");
  677.       fprintf (stderr, "\n");
  678.     }
  679.       fflush (stderr);
  680. #ifdef DEBUG
  681.       fprintf (stderr, "\nGo ahead? (y or n) ");
  682.       fflush (stderr);
  683.       j = getchar ();
  684.       if (j != '\n')
  685.     while (getchar () != '\n') ;
  686.       if (j != 'y' && j != 'Y')
  687.     return 0;
  688. #endif /* DEBUG */
  689.     }
  690.  
  691.   /* Run each piped subprocess.  */
  692.  
  693.   last_pipe_input = STDIN_FILE_NO;
  694.   for (i = 0; i < n_commands; i++)
  695.     {
  696.       extern int execv(), execvp();
  697.       char *string = commands[i].argv[0];
  698.  
  699.       commands[i].pid = pexecute ((string != commands[i].prog ? execv : execvp),
  700.                   string, commands[i].argv,
  701.                   i + 1 < n_commands);
  702.  
  703.       if (string != commands[i].prog)
  704.     free (string);
  705.     }
  706.  
  707.   execution_count++;
  708.  
  709.   /* Wait for all the subprocesses to finish.
  710.      We don't care what order they finish in;
  711.      we know that N_COMMANDS waits will get them all.  */
  712.  
  713.   {
  714.     int ret_code = 0;
  715.  
  716.     for (i = 0; i < n_commands; i++)
  717.       {
  718.     int status;
  719.     int pid;
  720.     char *prog;
  721.  
  722.     pid = wait (&status);
  723.     if (pid < 0)
  724.       abort ();
  725.  
  726.     if (status != 0)
  727.       {
  728.         int j;
  729.         for (j = 0; j < n_commands; j++)
  730.           if (commands[j].pid == pid)
  731.         prog = commands[j].prog;
  732.  
  733.         if ((status & 0x7F) != 0)
  734.           fatal ("Program %s got fatal signal %d.", prog, (status & 0x7F));
  735.         if (((status & 0xFF00) >> 8) >= MIN_FATAL_STATUS)
  736.           ret_code = -1;
  737.       }
  738.       }
  739.     return ret_code;
  740.   }
  741. }
  742.  
  743. void
  744. set_target_machine(name)
  745.   char *name;
  746. {
  747.   register target_machine *t;
  748.   int i;
  749.  
  750.   for (i = 0; ; i++)
  751.   {
  752.     t = &target_machines[i];
  753.     if (t->name == 0)
  754.       {
  755.       return;
  756.       }
  757.     if (strcmp(t->name, name) == 0)
  758.       {
  759.         target_name = t->info->name;
  760.     target = t->info;
  761.     return;
  762.       }
  763.   }
  764. }
  765.  
  766. /* Find all the switches given to us
  767.    and make a vector describing them.
  768.    The elements of the vector a strings, one per switch given.
  769.    If a switch uses the following argument, then the `part1' field
  770.    is the switch itself and the `part2' field is the following argument.
  771.    The `valid' field is nonzero if any spec has looked at this switch;
  772.    if it remains zero at the end of the run, it must be meaningless.  */
  773.  
  774. struct switchstr
  775. {
  776.   char *part1;
  777.   char *part2;
  778.   int valid;
  779. };
  780.  
  781. struct switchstr *switches;
  782.  
  783. int n_switches;
  784.  
  785. /* Also a vector of input files specified.  */
  786.  
  787. char **infiles;
  788.  
  789. int n_infiles;
  790.  
  791. /* And a vector of corresponding output files is made up later.  */
  792.  
  793. char **outfiles;
  794.  
  795. /* Create the vector `switches' and its contents.
  796.    Store its length in `n_switches'.  */
  797.  
  798. void
  799. process_command (argc, argv)
  800.      int argc;
  801.      char **argv;
  802. {
  803.   extern char *getenv ();
  804.   register int i;
  805.   n_switches = 0;
  806.   n_infiles = 0;
  807.  
  808.   env_exec_prefix = getenv ("GCC_EXEC_PREFIX");
  809.  
  810.   /* Scan argv twice.  Here, the first time, just count how many switches
  811.      there will be in their vector, and how many input files in theirs.
  812.      Here we also parse the switches that cc itself uses (e.g. -v).  */
  813.  
  814.   for (i = 1; i < argc; i++)
  815.     {
  816.       if (argv[i][0] == '-' && argv[i][1] != 'l')
  817.     {
  818.       register char *p = &argv[i][1];
  819.       register int c = *p;
  820.  
  821.       switch (c)
  822.         {
  823.         case 'b':
  824.           machine_suffix = p + 1;
  825.           break;
  826.  
  827.         case 'B':
  828.           user_exec_prefix = p + 1;
  829.           break;
  830.  
  831.         case 'v':    /* Print our subcommands and print versions.  */
  832.           vflag++;
  833.           n_switches++;
  834.           break;
  835.  
  836.         case 'm':
  837.           /*
  838.            * See if this switch identifies a known target machine.
  839.            * If so, use it to set the list of specs for the target.
  840.            */
  841.           set_target_machine(p + 1);
  842.           n_switches++;
  843.           break;
  844.  
  845.         default:
  846.           n_switches++;
  847.  
  848.           if (SWITCH_TAKES_ARG (c) && p[1] == 0)
  849.         i++;
  850.           else if (WORD_SWITCH_TAKES_ARG (p))
  851.         i++;
  852.         }
  853.     }
  854.       else
  855.     n_infiles++;
  856.     }
  857.  
  858.   /* Then create the space for the vectors and scan again.  */
  859.  
  860.   switches = ((struct switchstr *)
  861.           xmalloc ((n_switches + 1) * sizeof (struct switchstr)));
  862.   infiles = (char **) xmalloc ((n_infiles + 1) * sizeof (char *));
  863.   n_switches = 0;
  864.   n_infiles = 0;
  865.  
  866.   /* This, time, copy the text of each switch and store a pointer
  867.      to the copy in the vector of switches.
  868.      Store all the infiles in their vector.  */
  869.  
  870.   for (i = 1; i < argc; i++)
  871.     {
  872.       if (argv[i][0] == '-' && argv[i][1] != 'l')
  873.     {
  874.       register char *p = &argv[i][1];
  875.       register int c = *p;
  876.  
  877.       if (c == 'b')
  878.         continue;
  879.       switches[n_switches].part1 = p;
  880.       if ((SWITCH_TAKES_ARG (c) && p[1] == 0)
  881.           || WORD_SWITCH_TAKES_ARG (p))
  882.         switches[n_switches].part2 = argv[++i];
  883.       else
  884.         switches[n_switches].part2 = 0;
  885.       switches[n_switches].valid = 0;
  886.       if (c != 'B')
  887.           n_switches++;
  888.     }
  889.       else
  890.     infiles[n_infiles++] = argv[i];
  891.     }
  892.  
  893.   switches[n_switches].part1 = 0;
  894.   infiles[n_infiles] = 0;
  895. }
  896.  
  897. /* Process a spec string, accumulating and running commands.  */
  898.  
  899. /* These variables describe the input file name.
  900.    input_file_number is the index on outfiles of this file,
  901.    so that the output file name can be stored for later use by %o.
  902.    input_basename is the start of the part of the input file
  903.    sans all directory names, and basename_length is the number
  904.    of characters starting there excluding the suffix .c or whatever.  */
  905.  
  906. char *input_filename;
  907. int input_file_number;
  908. int input_filename_length;
  909. int basename_length;
  910. char *input_basename;
  911.  
  912. /* These are variables used within do_spec and do_spec_1.  */
  913.  
  914. /* Nonzero if an arg has been started and not yet terminated
  915.    (with space, tab or newline).  */
  916. int arg_going;
  917.  
  918. /* Nonzero means %d or %g has been seen; the next arg to be terminated
  919.    is a temporary file name.  */
  920. int delete_this_arg;
  921.  
  922. /* Nonzero means %w has been seen; the next arg to be terminated
  923.    is the output file name of this compilation.  */
  924. int this_is_output_file;
  925.  
  926. /* Nonzero means %s has been seen; the next arg to be terminated
  927.    is the name of a library file and we should try the standard
  928.    search dirs for it.  */
  929. int this_is_library_file;
  930.  
  931. /* Process the spec SPEC and run the commands specified therein.
  932.    Returns 0 if the spec is successfully processed; -1 if failed.  */
  933.  
  934. int
  935. do_spec (spec)
  936.      char *spec;
  937. {
  938.   int value;
  939.  
  940.   clear_args ();
  941.   arg_going = 0;
  942.   delete_this_arg = 0;
  943.   this_is_output_file = 0;
  944.   this_is_library_file = 0;
  945.  
  946.   value = do_spec_1 (spec, 0);
  947.  
  948.   /* Force out any unfinished command.
  949.      If -pipe, this forces out the last command if it ended in `|'.  */
  950.   if (value == 0)
  951.     {
  952.       if (argbuf_index > 0 && !strcmp (argbuf[argbuf_index - 1], "|"))
  953.     argbuf_index--;
  954.  
  955.       if (argbuf_index > 0)
  956.     value = execute ();
  957.     }
  958.  
  959.   return value;
  960. }
  961.  
  962. /* Process the sub-spec SPEC as a portion of a larger spec.
  963.    This is like processing a whole spec except that we do
  964.    not initialize at the beginning and we do not supply a
  965.    newline by default at the end.
  966.    INSWITCH nonzero means don't process %-sequences in SPEC;
  967.    in this case, % is treated as an ordinary character.
  968.    This is used while substituting switches.
  969.    INSWITCH nonzero also causes SPC not to terminate an argument.
  970.  
  971.    Value is zero unless a line was finished
  972.    and the command on that line reported an error.  */
  973.  
  974. int
  975. do_spec_1 (spec, inswitch)
  976.      char *spec;
  977.      int inswitch;
  978. {
  979.   register char *p = spec;
  980.   register int c;
  981.   char *string;
  982.  
  983.   while (c = *p++)
  984.     /* If substituting a switch, treat all chars like letters.
  985.        Otherwise, NL, SPC, TAB and % are special.  */
  986.     switch (inswitch ? 'a' : c)
  987.       {
  988.       case '\n':
  989.     /* End of line: finish any pending argument,
  990.        then run the pending command if one has been started.  */
  991.     if (arg_going)
  992.       {
  993.         obstack_1grow (&obstack, 0);
  994.         string = obstack_finish (&obstack);
  995.         if (this_is_library_file)
  996.           string = find_file (string);
  997.         store_arg (string, delete_this_arg, this_is_output_file);
  998.         if (this_is_output_file)
  999.           outfiles[input_file_number] = string;
  1000.       }
  1001.     arg_going = 0;
  1002.  
  1003.     if (argbuf_index > 0 && !strcmp (argbuf[argbuf_index - 1], "|"))
  1004.       {
  1005.         int i;
  1006.         for (i = 0; i < n_switches; i++)
  1007.           if (!strcmp (switches[i].part1, "pipe"))
  1008.         break;
  1009.  
  1010.         /* A `|' before the newline means use a pipe here,
  1011.            but only if -pipe was specified.
  1012.            Otherwise, execute now and don't pass the `|' as an arg.  */
  1013.         if (i < n_switches)
  1014.           {
  1015.         switches[i].valid = 1;
  1016.         break;
  1017.           }
  1018.         else
  1019.           argbuf_index--;
  1020.       }
  1021.  
  1022.     if (argbuf_index > 0)
  1023.       {
  1024.         int value = execute ();
  1025.         if (value)
  1026.           return value;
  1027.       }
  1028.     /* Reinitialize for a new command, and for a new argument.  */
  1029.     clear_args ();
  1030.     arg_going = 0;
  1031.     delete_this_arg = 0;
  1032.     this_is_output_file = 0;
  1033.     this_is_library_file = 0;
  1034.     break;
  1035.  
  1036.       case '|':
  1037.     /* End any pending argument.  */
  1038.     if (arg_going)
  1039.       {
  1040.         obstack_1grow (&obstack, 0);
  1041.         string = obstack_finish (&obstack);
  1042.         if (this_is_library_file)
  1043.           string = find_file (string);
  1044.         store_arg (string, delete_this_arg, this_is_output_file);
  1045.         if (this_is_output_file)
  1046.           outfiles[input_file_number] = string;
  1047.       }
  1048.  
  1049.     /* Use pipe */
  1050.     obstack_1grow (&obstack, c);
  1051.     arg_going = 1;
  1052.     break;
  1053.  
  1054.       case '\t':
  1055.       case ' ':
  1056.     /* Space or tab ends an argument if one is pending.  */
  1057.     if (arg_going)
  1058.       {
  1059.         obstack_1grow (&obstack, 0);
  1060.         string = obstack_finish (&obstack);
  1061.         if (this_is_library_file)
  1062.           string = find_file (string);
  1063.         store_arg (string, delete_this_arg, this_is_output_file);
  1064.         if (this_is_output_file)
  1065.           outfiles[input_file_number] = string;
  1066.       }
  1067.     /* Reinitialize for a new argument.  */
  1068.     arg_going = 0;
  1069.     delete_this_arg = 0;
  1070.     this_is_output_file = 0;
  1071.     this_is_library_file = 0;
  1072.     break;
  1073.  
  1074.       case '%':
  1075.     switch (c = *p++)
  1076.       {
  1077.       case 0:
  1078.         fatal ("Invalid specification!  Bug in cc.");
  1079.  
  1080.       case 'b':
  1081.         obstack_grow (&obstack, input_basename, basename_length);
  1082.         arg_going = 1;
  1083.         break;
  1084.  
  1085.       case 'd':
  1086.         delete_this_arg = 2;
  1087.         break;
  1088.  
  1089.       case 'e':
  1090.         /* {...:%efoo} means report an error with `foo' as error message
  1091.            and don't execute any more commands for this file.  */
  1092.         {
  1093.           char *q = p;
  1094.           char *buf;
  1095.           while (*p != 0 && *p != '\n') p++;
  1096.           buf = (char *) alloca (p - q + 1);
  1097.           strncpy (buf, q, p - q);
  1098.           error ("%s", buf);
  1099.           return -1;
  1100.         }
  1101.         break;
  1102.  
  1103.       case 'g':
  1104.         obstack_grow (&obstack, temp_filename, temp_filename_length);
  1105.         delete_this_arg = 1;
  1106.         arg_going = 1;
  1107.         break;
  1108.  
  1109.       case 'i':
  1110.         obstack_grow (&obstack, input_filename, input_filename_length);
  1111.         arg_going = 1;
  1112.         break;
  1113.  
  1114.       case 'o':
  1115.         {
  1116.           register int f;
  1117.           for (f = 0; f < n_infiles; f++)
  1118.         store_arg (outfiles[f], 0, 0);
  1119.         }
  1120.         break;
  1121.  
  1122.       case 's':
  1123.         this_is_library_file = 1;
  1124.         break;
  1125.  
  1126.       case 'w':
  1127.         this_is_output_file = 1;
  1128.         break;
  1129.  
  1130.       case '{':
  1131.         p = handle_braces (p);
  1132.         if (p == 0)
  1133.           return -1;
  1134.         break;
  1135.  
  1136.       case '%':
  1137.         obstack_1grow (&obstack, '%');
  1138.         break;
  1139.  
  1140. /*** The rest just process a certain constant string as a spec.  */
  1141.  
  1142.           case '+':
  1143.         do_spec_1 (target->cplus1_spec, 0);
  1144.         break;
  1145.  
  1146.       case '1':
  1147.         do_spec_1 (target->cc1_spec, 0);
  1148.         break;
  1149.  
  1150.       case 'a':
  1151.         do_spec_1 (target->asm_spec, 0);
  1152.         break;
  1153.  
  1154.       case 'c':
  1155.         do_spec_1 (target->signed_char_spec, 0);
  1156.         break;
  1157.  
  1158.       case 'C':
  1159.         do_spec_1 (target->cpp_spec, 0);
  1160.         break;
  1161.  
  1162.       case 'l':
  1163.         do_spec_1 (target->link_spec, 0);
  1164.         break;
  1165.  
  1166.       case 'L':
  1167.         do_spec_1 (target->lib_spec, 0);
  1168.         break;
  1169.  
  1170.       case 'm':
  1171.         obstack_grow (&obstack, target_name, strlen (target_name));
  1172.         arg_going = 1;
  1173.         break;
  1174.  
  1175.       case 'p':
  1176.         do_spec_1 (target->cpp_predefines, 0);
  1177.         break;
  1178.  
  1179.       case 'P':
  1180.         {
  1181.           char *x = (char *) alloca(strlen(target->cpp_predefines) * 2 + 1);
  1182.           char *buf = x;
  1183.           char *y = target->cpp_predefines;
  1184.  
  1185.           /* Copy all of target->cpp_predefines into BUF,
  1186.          but put __ after every -D and at the end of each arg,  */
  1187.           while (1)
  1188.         {
  1189.           if (! strncmp (y, "-D", 2))
  1190.             {
  1191.               *x++ = '-';
  1192.               *x++ = 'D';
  1193.               *x++ = '_';
  1194.               *x++ = '_';
  1195.               y += 2;
  1196.             }
  1197.           else if (*y == ' ' || *y == 0)
  1198.             {
  1199.               *x++ = '_';
  1200.               *x++ = '_';
  1201.               if (*y == 0)
  1202.             break;
  1203.               else
  1204.             *x++ = *y++;
  1205.             }
  1206.           else
  1207.             *x++ = *y++;
  1208.         }
  1209.           *x = 0;
  1210.  
  1211.           do_spec_1 (buf, 0);
  1212.         }
  1213.         break;
  1214.  
  1215.       case 'S':
  1216.         do_spec_1 (target->startfile_spec, 0);
  1217.         break;
  1218.  
  1219.       default:
  1220.         fatal ("Invalid spec sequence \"%%%c\"!  Bug in cc.", c);
  1221.       }
  1222.     break;
  1223.  
  1224.       default:
  1225.     /* Ordinary character: put it into the current argument.  */
  1226.     obstack_1grow (&obstack, c);
  1227.     arg_going = 1;
  1228.       }
  1229.  
  1230.   return 0;        /* End of string */
  1231. }
  1232.  
  1233. /* Return 0 if we call do_spec_1 and that returns -1.  */
  1234.  
  1235. char *
  1236. handle_braces (p)
  1237.      register char *p;
  1238. {
  1239.   register char *q;
  1240.   char *filter;
  1241.   int pipe = 0;
  1242.   int negate = 0;
  1243.  
  1244.   if (*p == '|')
  1245.     /* A `|' after the open-brace means,
  1246.        if the test fails, output a single minus sign rather than nothing.
  1247.        This is used in %{|!pipe:...}.  */
  1248.     pipe = 1, ++p;
  1249.  
  1250.   if (*p == '!')
  1251.     /* A `!' after the open-brace negates the condition:
  1252.        succeed if the specified switch is not present.  */
  1253.     negate = 1, ++p;
  1254.  
  1255.   filter = p;
  1256.   while (*p != ':' && *p != '}') p++;
  1257.   if (*p != '}')
  1258.     {
  1259.       register int count = 1;
  1260.       q = p + 1;
  1261.       while (count > 0)
  1262.     {
  1263.       if (*q == '{')
  1264.         count++;
  1265.       else if (*q == '}')
  1266.         count--;
  1267.       else if (*q == 0)
  1268.         abort ();
  1269.       q++;
  1270.     }
  1271.     }
  1272.   else
  1273.     q = p + 1;
  1274.  
  1275.   if (p[-1] == '*' && p[0] == '}')
  1276.     {
  1277.       /* Substitute all matching switches as separate args.  */
  1278.       register int i;
  1279.       --p;
  1280.       for (i = 0; i < n_switches; i++)
  1281.     if (!strncmp (switches[i].part1, filter, p - filter))
  1282.       give_switch (i);
  1283.     }
  1284.   else
  1285.     {
  1286.       /* Test for presence of the specified switch.  */
  1287.       register int i;
  1288.       int present = 0;
  1289.  
  1290.       /* If name specified ends in *, as in {x*:...},
  1291.      check for presence of any switch name starting with x.  */
  1292.       if (p[-1] == '*')
  1293.     {
  1294.       for (i = 0; i < n_switches; i++)
  1295.         {
  1296.           if (!strncmp (switches[i].part1, filter, p - filter - 1))
  1297.         {
  1298.           switches[i].valid = 1;
  1299.           present = 1;
  1300.         }
  1301.         }
  1302.     }
  1303.       /* Otherwise, check for presence of exact name specified.  */
  1304.       else
  1305.     {
  1306.       for (i = 0; i < n_switches; i++)
  1307.         {
  1308.           if (!strncmp (switches[i].part1, filter, p - filter)
  1309.           && switches[i].part1[p - filter] == 0)
  1310.         {
  1311.           switches[i].valid = 1;
  1312.           present = 1;
  1313.           break;
  1314.         }
  1315.         }
  1316.     }
  1317.  
  1318.       /* If it is as desired (present for %{s...}, absent for %{-s...})
  1319.      then substitute either the switch or the specified
  1320.      conditional text.  */
  1321.       if (present != negate)
  1322.     {
  1323.       if (*p == '}')
  1324.         {
  1325.           give_switch (i);
  1326.         }
  1327.       else
  1328.         {
  1329.           if (do_spec_1 (save_string (p + 1, q - p - 2), 0) < 0)
  1330.         return 0;
  1331.         }
  1332.     }
  1333.       else if (pipe)
  1334.     {
  1335.       /* Here if a %{|...} conditional fails: output a minus sign,
  1336.          which means "standard output" or "standard input".  */
  1337.       do_spec_1 ("-", 0);
  1338.     }
  1339.     }
  1340.  
  1341.   return q;
  1342. }
  1343.  
  1344. /* Pass a switch to the current accumulating command
  1345.    in the same form that we received it.
  1346.    SWITCHNUM identifies the switch; it is an index into
  1347.    the vector of switches gcc received, which is `switches'.
  1348.    This cannot fail since it never finishes a command line.  */
  1349.  
  1350. give_switch (switchnum)
  1351.      int switchnum;
  1352. {
  1353.   do_spec_1 ("-", 0);
  1354.   do_spec_1 (switches[switchnum].part1, 1);
  1355.   do_spec_1 (" ", 0);
  1356.   if (switches[switchnum].part2 != 0)
  1357.     {
  1358.       do_spec_1 (switches[switchnum].part2, 1);
  1359.       do_spec_1 (" ", 0);
  1360.     }
  1361.   switches[switchnum].valid = 1;
  1362. }
  1363.  
  1364. /* Search for a file named NAME trying various prefixes including the
  1365.    user's -B prefix and some standard ones.
  1366.    Return the absolute pathname found.  If nothing is found, return NAME.  */
  1367.  
  1368. char *
  1369. find_file (name)
  1370.      char *name;
  1371. {
  1372.   int size;
  1373.   char *temp;
  1374.   int win = 0;
  1375.  
  1376.   /* Compute maximum size of NAME plus any prefix we will try.  */
  1377.  
  1378.   size = strlen (standard_exec_prefix);
  1379.   if (user_exec_prefix != 0 && strlen (user_exec_prefix) > size)
  1380.     size = strlen (user_exec_prefix);
  1381.   if (env_exec_prefix != 0 && strlen (env_exec_prefix) > size)
  1382.     size = strlen (env_exec_prefix);
  1383.   if (strlen (standard_exec_prefix) > size)
  1384.     size = strlen (standard_exec_prefix);
  1385.   if (strlen (standard_exec_prefix_1) > size)
  1386.     size = strlen (standard_exec_prefix_1);
  1387.   if (strlen (standard_startfile_prefix) > size)
  1388.     size = strlen (standard_startfile_prefix);
  1389.   if (strlen (standard_startfile_prefix_1) > size)
  1390.     size = strlen (standard_startfile_prefix_1);
  1391.   size += strlen (name) + strlen (target_name) + 1;
  1392.  
  1393.   temp = (char *) alloca (size);
  1394.  
  1395.   if (user_exec_prefix)
  1396.     {
  1397.       sprintf (temp, user_exec_prefix, target_name);
  1398.       strcat (temp, name);
  1399.       win = (access (temp, R_OK) == 0);
  1400.     }
  1401.  
  1402.   if (!win && env_exec_prefix)
  1403.     {
  1404.       sprintf (temp, env_exec_prefix, target_name);
  1405.       strcat (temp, name);
  1406.       win = (access (temp, R_OK) == 0);
  1407.     }
  1408.  
  1409.   if (!win)
  1410.     {
  1411.       sprintf (temp, standard_exec_prefix, target_name);
  1412.       strcat (temp, name);
  1413.       win = (access (temp, R_OK) == 0);
  1414.     }
  1415.  
  1416.   if (!win)
  1417.     {
  1418.       sprintf (temp, standard_exec_prefix_1, target_name);
  1419.       strcat (temp, name);
  1420.       win = (access (temp, R_OK) == 0);
  1421.     }
  1422.  
  1423.   if (!win)
  1424.     {
  1425.       sprintf (temp, standard_startfile_prefix, target_name);
  1426.       strcat (temp, name);
  1427.       win = (access (temp, R_OK) == 0);
  1428.     }
  1429.  
  1430.   if (!win)
  1431.     {
  1432.       sprintf (temp, standard_startfile_prefix_1, target_name);
  1433.       strcat (temp, name);
  1434.       win = (access (temp, R_OK) == 0);
  1435.     }
  1436.  
  1437.   if (!win)
  1438.     {
  1439.     strcpy (temp, "./");
  1440.     strcat (temp, name);
  1441.     win = (access (temp, R_OK) == 0);
  1442.     }
  1443.  
  1444.   if (win)
  1445.     return save_string (temp, strlen (temp));
  1446.   return name;
  1447. }
  1448.  
  1449. /* On fatal signals, delete all the temporary files.  */
  1450.  
  1451. void
  1452. fatal_error (signum)
  1453.      int signum;
  1454. {
  1455.   signal (signum, SIG_DFL);
  1456.   delete_failure_queue ();
  1457.   delete_temp_files ();
  1458.   /* Get the same signal again, this time not handled,
  1459.      so its normal effect occurs.  */
  1460.   kill (getpid (), signum);
  1461. }
  1462.  
  1463. int
  1464. main (argc, argv)
  1465.      int argc;
  1466.      char **argv;
  1467. {
  1468.   register int i;
  1469.   int value;
  1470.   int error_count = 0;
  1471.   int linker_was_run = 0;
  1472.   char *explicit_link_files;
  1473.   extern char *getenv();
  1474.   char *basename;
  1475.  
  1476.   programname = argv[0];
  1477.   if ((basename = strrchr(programname, '/')) != NULL) {
  1478.       ++basename;
  1479.   } else {
  1480.       basename = programname;
  1481.   }
  1482.   if (strcmp(basename, "g++") == 0) {
  1483.       cplusplus = 1;
  1484.   }
  1485.  
  1486.   if (signal (SIGINT, SIG_IGN) != SIG_IGN)
  1487.     signal (SIGINT, fatal_error);
  1488.   if (signal (SIGHUP, SIG_IGN) != SIG_IGN)
  1489.     signal (SIGHUP, fatal_error);
  1490.   if (signal (SIGTERM, SIG_IGN) != SIG_IGN)
  1491.     signal (SIGTERM, fatal_error);
  1492.  
  1493.   argbuf_length = 10;
  1494.   argbuf = (char **) xmalloc (argbuf_length * sizeof (char *));
  1495.  
  1496.   obstack_init (&obstack);
  1497.  
  1498.   choose_temp_base ();
  1499.  
  1500.   /* Make a table of what switches there are (switches, n_switches).
  1501.      Make a table of specified input files (infiles, n_infiles).  */
  1502.  
  1503.   process_command (argc, argv);
  1504.  
  1505.   /* Figure out the machine type we're running on, using the $MACHINE
  1506.     environment variable. */
  1507.  
  1508.   machine = getenv("MACHINE");
  1509.   if (machine == NULL)
  1510.     {
  1511.       machine = "";
  1512.     }
  1513.     machine_length = strlen(machine);
  1514.  
  1515.   /* If the target machine wasn't specified in a switch, then get it from
  1516.      the TM environment variable, if it exists, or next from the MACHINE
  1517.      environment variable.  If none of these exists, pick a default. */
  1518.  
  1519.    if (target == NULL)
  1520.      {
  1521.        char *env;
  1522.  
  1523.        env = getenv ("TM");
  1524.        if (env == NULL)
  1525.          {
  1526.        env = getenv ("MACHINE");
  1527.        if (env == NULL)
  1528.          {
  1529.            env = target_machines[0].name;
  1530.          }
  1531.      }
  1532.        set_target_machine (env);
  1533.      }
  1534.  
  1535.   if (target == NULL) {
  1536.       fprintf(stderr, "No target machine specified\n");
  1537.       exit(1);
  1538.   }
  1539.  
  1540.   if (vflag)
  1541.     {
  1542.       extern char *version_string;
  1543.       fprintf (stderr, "gcc version %s\n", version_string);
  1544.       fprintf (stderr, "target machine is %s\n", target_name);      
  1545.       if (n_infiles == 0)
  1546.     exit (0);
  1547.     }
  1548.  
  1549.   if (n_infiles == 0)
  1550.     fatal ("No input files specified.");
  1551.  
  1552.   /* Make a place to record the compiler output file names
  1553.      that correspond to the input files.  */
  1554.  
  1555.   outfiles = (char **) xmalloc (n_infiles * sizeof (char *));
  1556.   bzero (outfiles, n_infiles * sizeof (char *));
  1557.  
  1558.   /* Record which files were specified explicitly as link input.  */
  1559.  
  1560.   explicit_link_files = (char *) xmalloc (n_infiles);
  1561.   bzero (explicit_link_files, n_infiles);
  1562.  
  1563.   for (i = 0; i < n_infiles; i++)
  1564.     {
  1565.       register struct compiler *cp;
  1566.       int this_file_error = 0;
  1567.  
  1568.       /* Tell do_spec what to substitute for %i.  */
  1569.  
  1570.       input_filename = infiles[i];
  1571.       input_filename_length = strlen (input_filename);
  1572.       input_file_number = i;
  1573.  
  1574.       /* Use the same thing in %o, unless cp->spec says otherwise.  */
  1575.  
  1576.       outfiles[i] = input_filename;
  1577.  
  1578.       /* Figure out which compiler from the file's suffix.  */
  1579.  
  1580.       for (cp = (cplusplus ? target->cplusplus_specs : target->base_specs);
  1581.            cp->spec; cp++)
  1582.     {
  1583.       if (strlen (cp->suffix) < input_filename_length
  1584.           && !strcmp (cp->suffix,
  1585.               infiles[i] + input_filename_length
  1586.               - strlen (cp->suffix)))
  1587.         {
  1588.           /* Ok, we found an applicable compiler.  Run its spec.  */
  1589.           /* First say how much of input_filename to substitute for %b  */
  1590.           register char *p;
  1591.  
  1592.           input_basename = input_filename;
  1593.           for (p = input_filename; *p; p++)
  1594.         if (*p == '/')
  1595.           input_basename = p + 1;
  1596.           basename_length = (input_filename_length - strlen (cp->suffix)
  1597.                  - (input_basename - input_filename));
  1598.           value = do_spec (cp->spec);
  1599.           if (value < 0)
  1600.         this_file_error = 1;
  1601.           break;
  1602.         }
  1603.     }
  1604.  
  1605.       /* If this file's name does not contain a recognized suffix,
  1606.      record it as explicit linker input.  */
  1607.  
  1608.       if (! cp->spec)
  1609.     explicit_link_files[i] = 1;
  1610.  
  1611.       /* Clear the delete-on-failure queue, deleting the files in it
  1612.      if this compilation failed.  */
  1613.  
  1614.       if (this_file_error)
  1615.     {
  1616.       delete_failure_queue ();
  1617.       error_count++;
  1618.     }
  1619.       /* If this compilation succeeded, don't delete those files later.  */
  1620.       clear_failure_queue ();
  1621.     }
  1622.  
  1623.   /* Run ld to link all the compiler output files.  */
  1624.  
  1625.   if (error_count == 0)
  1626.     {
  1627.       int tmp = execution_count;
  1628.       value = do_spec (target->link_base_spec);
  1629.       if (value < 0)
  1630.     error_count = 1;
  1631.       linker_was_run = (tmp != execution_count);
  1632.     }
  1633.  
  1634.   /* If options said don't run linker,
  1635.      complain about input files to be given to the linker.  */
  1636.  
  1637.   if (! linker_was_run && error_count == 0)
  1638.     for (i = 0; i < n_infiles; i++)
  1639.       if (explicit_link_files[i])
  1640.     error ("%s: linker input file unused since linking not done",
  1641.            outfiles[i]);
  1642.  
  1643.   /* Set the `valid' bits for switches that match anything in any spec.  */
  1644.  
  1645.   validate_all_switches ();
  1646.  
  1647.   /* Warn about any switches that no pass was interested in.  */
  1648.   
  1649.   for (i = 0; i < n_switches; i++)
  1650.     if (! switches[i].valid)
  1651.       error ("unrecognized option `-%s'", switches[i].part1);
  1652.  
  1653.   /* Delete some or all of the temporary files we made.  */
  1654.  
  1655.   if (error_count)
  1656.     delete_failure_queue ();
  1657.   delete_temp_files ();
  1658.  
  1659.   exit (error_count);
  1660. }
  1661.  
  1662. xmalloc (size)
  1663.      int size;
  1664. {
  1665.   register int value = malloc (size);
  1666.   if (value == 0)
  1667.     fatal ("Virtual memory full.");
  1668.   return value;
  1669. }
  1670.  
  1671. xrealloc (ptr, size)
  1672.      int ptr, size;
  1673. {
  1674.   register int value = realloc (ptr, size);
  1675.   if (value == 0)
  1676.     fatal ("Virtual memory full.");
  1677.   return value;
  1678. }
  1679.  
  1680. /* Return a newly-allocated string whose contents concatenate those of s1, s2, s3.  */
  1681.  
  1682. char *
  1683. concat (s1, s2, s3)
  1684.      char *s1, *s2, *s3;
  1685. {
  1686.   int len1 = strlen (s1), len2 = strlen (s2), len3 = strlen (s3);
  1687.   char *result = (char *) xmalloc (len1 + len2 + len3 + 1);
  1688.  
  1689.   strcpy (result, s1);
  1690.   strcpy (result + len1, s2);
  1691.   strcpy (result + len1 + len2, s3);
  1692.   *(result + len1 + len2 + len3) = 0;
  1693.  
  1694.   return result;
  1695. }
  1696.  
  1697. char *
  1698. save_string (s, len)
  1699.      char *s;
  1700.      int len;
  1701. {
  1702.   register char *result = (char *) xmalloc (len + 1);
  1703.  
  1704.   bcopy (s, result, len);
  1705.   result[len] = 0;
  1706.   return result;
  1707. }
  1708.  
  1709. pfatal_with_name (name)
  1710.      char *name;
  1711. {
  1712.   extern int errno, sys_nerr;
  1713.   extern char *sys_errlist[];
  1714.   char *s;
  1715.  
  1716.   if (errno < sys_nerr)
  1717.     s = concat ("%s: ", sys_errlist[errno], "");
  1718.   else
  1719.     s = "cannot open %s";
  1720.   fatal (s, name);
  1721. }
  1722.  
  1723. perror_with_name (name)
  1724.      char *name;
  1725. {
  1726.   extern int errno, sys_nerr;
  1727.   extern char *sys_errlist[];
  1728.   char *s;
  1729.  
  1730.   if (errno < sys_nerr)
  1731.     s = concat ("%s: ", sys_errlist[errno], "");
  1732.   else
  1733.     s = "cannot open %s";
  1734.   error (s, name);
  1735. }
  1736.  
  1737. perror_exec (name)
  1738.      char *name;
  1739. {
  1740.   extern int errno, sys_nerr;
  1741.   extern char *sys_errlist[];
  1742.   char *s;
  1743.  
  1744.   if (errno < sys_nerr)
  1745.     s = concat ("installation problem, cannot exec %s: ",
  1746.         sys_errlist[errno], "");
  1747.   else
  1748.     s = "installation problem, cannot exec %s";
  1749.   error (s, name);
  1750. }
  1751.  
  1752. /* More 'friendly' abort that prints the line and file.
  1753.    config.h can #define abort fancy_abort if you like that sort of thing.  */
  1754.  
  1755. void
  1756. fancy_abort ()
  1757. {
  1758.   fatal ("Internal gcc abort.");
  1759. }
  1760.  
  1761. #ifdef HAVE_VPRINTF
  1762.  
  1763. /* Output an error message and exit */
  1764.  
  1765. int 
  1766. fatal (va_alist)
  1767.      va_dcl
  1768. {
  1769.   va_list ap;
  1770.   char *format;
  1771.   
  1772.   va_start(ap);
  1773.   format = va_arg (ap, char *);
  1774.   vfprintf (stderr, format, ap);
  1775.   va_end (ap);
  1776.   fprintf (stderr, "\n");
  1777.   delete_temp_files (0);
  1778.   exit (1);
  1779. }  
  1780.  
  1781. error (va_alist) 
  1782.      va_dcl
  1783. {
  1784.   va_list ap;
  1785.   char *format;
  1786.  
  1787.   va_start(ap);
  1788.   format = va_arg (ap, char *);
  1789.   fprintf (stderr, "%s: ", programname);
  1790.   vfprintf (stderr, format, ap);
  1791.   va_end (ap);
  1792.  
  1793.   fprintf (stderr, "\n");
  1794. }
  1795.  
  1796. #else /* not HAVE_VPRINTF */
  1797.  
  1798. fatal (msg, arg1, arg2)
  1799.      char *msg, *arg1, *arg2;
  1800. {
  1801.   error (msg, arg1, arg2);
  1802.   delete_temp_files (0);
  1803.   exit (1);
  1804. }
  1805.  
  1806. error (msg, arg1, arg2)
  1807.      char *msg, *arg1, *arg2;
  1808. {
  1809.   fprintf (stderr, "%s: ", programname);
  1810.   fprintf (stderr, msg, arg1, arg2);
  1811.   fprintf (stderr, "\n");
  1812. }
  1813.  
  1814. #endif /* not HAVE_VPRINTF */
  1815.  
  1816.  
  1817. void
  1818. validate_all_switches ()
  1819. {
  1820.   struct compiler *comp;
  1821.   register char *p;
  1822.   register char c;
  1823.  
  1824.   for (comp = (cplusplus ? target->cplusplus_specs : target->base_specs);
  1825.        comp->spec; comp++)
  1826.     {
  1827.       p = comp->spec;
  1828.       while (c = *p++)
  1829.     if (c == '%' && *p == '{')
  1830.       /* We have a switch spec.  */
  1831.       validate_switches (p + 1);
  1832.     }
  1833.  
  1834.   p = target->link_base_spec;
  1835.   while (c = *p++)
  1836.     if (c == '%' && *p == '{')
  1837.       /* We have a switch spec.  */
  1838.       validate_switches (p + 1);
  1839.  
  1840.   /* Now notice switches mentioned in the machine-specific specs.  */
  1841.  
  1842. #ifdef ASM_SPEC
  1843.   p = ASM_SPEC;
  1844.   while (c = *p++)
  1845.     if (c == '%' && *p == '{')
  1846.       /* We have a switch spec.  */
  1847.       validate_switches (p + 1);
  1848. #endif
  1849.  
  1850. #ifdef CPP_SPEC
  1851.   p = CPP_SPEC;
  1852.   while (c = *p++)
  1853.     if (c == '%' && *p == '{')
  1854.       /* We have a switch spec.  */
  1855.       validate_switches (p + 1);
  1856. #endif
  1857.  
  1858. #ifdef SIGNED_CHAR_SPEC
  1859.   p = SIGNED_CHAR_SPEC;
  1860.   while (c = *p++)
  1861.     if (c == '%' && *p == '{')
  1862.       /* We have a switch spec.  */
  1863.       validate_switches (p + 1);
  1864. #endif
  1865.  
  1866. #ifdef CC1_SPEC
  1867.   p = CC1_SPEC;
  1868.   while (c = *p++)
  1869.     if (c == '%' && *p == '{')
  1870.       /* We have a switch spec.  */
  1871.       validate_switches (p + 1);
  1872. #endif
  1873.  
  1874. #ifdef LINK_SPEC
  1875.   p = LINK_SPEC;
  1876.   while (c = *p++)
  1877.     if (c == '%' && *p == '{')
  1878.       /* We have a switch spec.  */
  1879.       validate_switches (p + 1);
  1880. #endif
  1881.  
  1882. #ifdef LIB_SPEC
  1883.   p = LIB_SPEC;
  1884.   while (c = *p++)
  1885.     if (c == '%' && *p == '{')
  1886.       /* We have a switch spec.  */
  1887.       validate_switches (p + 1);
  1888. #endif
  1889.  
  1890. #ifdef STARTFILE_SPEC
  1891.   p = STARTFILE_SPEC;
  1892.   while (c = *p++)
  1893.     if (c == '%' && *p == '{')
  1894.       /* We have a switch spec.  */
  1895.       validate_switches (p + 1);
  1896. #endif
  1897. }
  1898.  
  1899. /* Look at the switch-name that comes after START
  1900.    and mark as valid all supplied switches that match it.  */
  1901.  
  1902. void
  1903. validate_switches (start)
  1904.      char *start;
  1905. {
  1906.   register char *p = start;
  1907.   char *filter;
  1908.   register int i;
  1909.  
  1910.   if (*p == '|')
  1911.     ++p;
  1912.  
  1913.   if (*p == '!')
  1914.     ++p;
  1915.  
  1916.   filter = p;
  1917.   while (*p != ':' && *p != '}') p++;
  1918.  
  1919.   if (p[-1] == '*')
  1920.     {
  1921.       /* Mark all matching switches as valid.  */
  1922.       --p;
  1923.       for (i = 0; i < n_switches; i++)
  1924.     if (!strncmp (switches[i].part1, filter, p - filter))
  1925.       switches[i].valid = 1;
  1926.     }
  1927.   else
  1928.     {
  1929.       /* Mark an exact matching switch as valid.  */
  1930.       for (i = 0; i < n_switches; i++)
  1931.     {
  1932.       if (!strncmp (switches[i].part1, filter, p - filter)
  1933.           && switches[i].part1[p - filter] == 0)
  1934.         switches[i].valid = 1;
  1935.     }
  1936.     }
  1937. }
  1938.